const dayjs = require('dayjs')
const _ = require('lodash')
const path = require('path')

module.exports = (app) => {
  app.jax.mongoose = {
    model: require('./model')(app),
    Schema: require('./schema')(app),
    formatDate(val, format = 'YYYY-MM-DD HH:mm:ss') {
      return val && dayjs(val).format(format)
    },

    /**
     * 获取模型名称
     * @param {*} param
     */
    getModelName(param) {
      const filename = path.parse(param).name
      return _.upperFirst(_.camelCase(filename))
    },

    // 预处理
    pre: {
      /**
       * 保存或修改获取序号
       * @param {Object} data
       */
      getRank(data) {
        if (data.rank != undefined) {
          if (data.rank) {
            data.isTop = true
          } else {
            data.isTop = false
          }
        }
      },
    },
  }
}
